import './CreateMemberTag.less'
import {Card, Input, Space, Form, Button, message} from 'antd'
import {Link} from 'react-router-dom'
import TextArea from "antd/es/input/TextArea";
import http from "../../../utils/http";
import {useNavigate} from "react-router";
const CreateMemberTag=()=>{
    const navigate = useNavigate();
    const [form] = Form.useForm();
    let data = {
        name:'',
        discount:'',
        overview:''
    }

    form.setFieldsValue(data);

    const addTagSubmit = () => {
        let flag = false;
        for (let key in data){
            if(data[key] == ''){
                flag = true;
                break;
            }
        }
        if(flag){
            message.warning('请填写完整！');
        }else {
            http({
                url:'api/user/insertLabel',
                method:'post',
                params:data
            }).then(res=>{
                if (res.code == 200){
                    message.success('添加成功');
                    navigate('/home/MemberTag');
                }else {
                    message.error(res.msg);
                }
            })
        }
    }

    return(
        <Card className='Cmt-Card'>
            <div className='header'>
                <h1>
                    标签信息
                </h1>
                <hr />
                <div className='cmt-form-body'>
                    <Form form={form} initialValues={data}>
                        <Form.Item name={'name'} label='标签名称' rules={[{required:true,message:'请输入标签信息'}]}>
                            <Input
                                value={data.name}
                                onChange={(ev)=>{data.name = ev.target.value}}
                                placeholder="请输入标签信息"
                                style={{width:'260px'}}
                            />
                        </Form.Item>
                        <Form.Item name={'overview'} label='标签描述' rules={[{required:true,message:'请输入等级描述,若没有请填‘无’'}]}>
                            <TextArea
                                showCount
                                value={data.overview}
                                placeholder={'请输入等级描述,若没有请填‘无’'}
                                maxLength={100}
                                style={{ width: 260}}
                                onChange={(ev)=>{data.overview = ev.target.value}} />
                        </Form.Item>
                    </Form>
                </div>
            </div>
            <div className='middle'>
                <h1>
                    等级权益
                </h1>
                <hr />
                <div className='cmt-form-body'>
                    <Form form={form} initialValues={data}>
                        <Form.Item name={'discount'} label='等级权益' rules={[{required:true,message:'请输入折扣（0-1）'}]}>
                            <Input
                                type={'digit'}
                                placeholder={'请输入折扣（0-1）'}
                                value={data.discount}
                                onChange={(ev)=>{data.discount = ev.target.value}}
                                addonAfter="折"
                                style={{width:'260px'}}
                            />
                        </Form.Item>
                    </Form>
                </div>
            </div>
            <div className='footer'>
                <div style={{width:'150px',display:'flex',justifyContent:'space-between'}}>
                    <div><Button type={'primary'} onClick={addTagSubmit}>添加</Button></div>
                    <div><Link to={'/home/MemberTag'}><Button>返回</Button></Link></div>
                </div>
            </div>
        </Card>
    )
}
export default CreateMemberTag;